package org.mms.service.impl;

import org.mms.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Value("${file.upload-dir}")
    private String uploadDir;

    @Value("${file.base-url}")
    private String baseUrl;

    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        // 创建存储目录
        File directory = new File(uploadDir);
        if (!directory.exists()) {
            directory.mkdirs();
        }

        String uniqueFileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + uploadDir + uniqueFileName;
        file.transferTo(new File(filePath));

        // 返回访问URL
        return baseUrl + uniqueFileName;
    }
}